// pages/Type/TypeDes/index.js
const common = require('../../../public.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isMore: true,
    goodsList: [], // 商品列表
    storeList: [], // 店铺列表
    queryString: '',
    goodsPageNum: 1,
    storePageNum: 1,
    not: true,
    storeNoMoreData: false,
    goodsNoMoreData: false,
    isLoadingStoreBusy: false,
    isLoadingGoodsBusy: false,
    option1: [
      { text: '店铺', value: 0 },
      { text: '商品', value: 1 },
    ],
    active: 0,
    search: '',
    searchAction: 0,
    history: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHistory()
  },

  dropdownMenuChange(e) {
    const active = e.detail
    this.setData({
      active,
      goodsList: [], // 商品列表
      storeList: [],
      searchAction: 0
    }, _ => {
      this.getHistory()
      this.onSearch(false)
    })
  },
  getHistory() {
    const history = wx.getStorageSync('history' + this.data.active) || []
    this.setData({
      history
    })
  },
  tagTap(e) {
    const search = e.currentTarget.dataset.search
    this.setData({
      search
    }, _ => {
      this.onSearch()
    })
  },
  onSearch(checkContent = true) {
    console.log(this.data.search)
    if (common.isEmptyString(this.data.search)) {
      checkContent && common.showToast('请输入要搜索的文字')
      return
    }
    // 更新历史记录
    const history = this.data.history || []
    // 查找是否有重复的数据
    for (let i = 0; i < history.length; i++) {
      const item = history[i]
      if (item == this.data.search) {
        history.splice(i, 1)
        break
      }
    }
    // 判断是否超过10条，超过则删除最后一个
    if (history.length == 10) {
      history.splice(9, 1)
    }
    history.unshift(this.data.search) //输入数据放到historyValue数组中
    this.setData({
      searchAction: 1
    }, _ => {
      this.saveHistory(history)
      this.refreshData()
    })
  },
  onClose(e) {
    const index = e.currentTarget.dataset.index
    const history = this.data.history || []
    history.splice(index, 1)
    this.saveHistory(history)
  },
  saveHistory(history) {
    this.setData({
      history
    })
    wx.setStorageSync('history' + this.data.active, history)//存储数据
  },
  refreshData() {
    if (this.data.active === 0) {
      this.setData({
        storeNoMoreData: false
      })
      this.getSearchList(true, false)
    } else {
      this.setData({
        goodsNoMoreData: false
      })
      this.getSearchList(true, true)
    }
  },
  /**
   * 获取搜索数据
   * @param {boolean} refresh 是否是刷新数据，默认是false
   * @param {boolean} getGoodsData 是否是获取的商品数据，默认是true
   */
  getSearchList(refresh = false, getGoodsData = true) {
    // 当前正在加载数据，不需要再次请求接口
    if (getGoodsData) {
      if (this.data.isLoadingGoodsBusy === true) {
        return
      }
    } else {
      if (this.data.isLoadingStoreBusy === true) {
        return
      }
    }

    const that = this
    // 判断当前操作是刷新还是加载更多数据
    let pageNum = 1
    if (!refresh) {
      if (getGoodsData) {
        // 当前页面已经没有更多数据了，不再请求接口
        if (this.data.goodsNoMoreData) {
          return
        }
        pageNum = this.data.goodsPageNum + 1
      } else {
        // 当前页面已经没有更多数据了，不再请求接口
        if (this.data.storeNoMoreData) {
          return
        }
        pageNum = this.data.storePageNum + 1
      }
    }
    if (getGoodsData) {
      this.setData({
        isLoadingGoodsBusy: true
      })
    } else {
      this.setData({
        isLoadingStoreBusy: true
      })
    }
    const tempJson = {
      queryString: that.data.search,
      page: pageNum
    }
    const url = getGoodsData ? '/es/search' : '/es/search/store'
    common.post(url, tempJson, function (res) {
      if (res.status === 200) {
        // 商品接口与店铺接口的返回值不同
        if (getGoodsData) {
          that.setData({
            goodsPageNum: pageNum
          })
        } else {
          that.setData({
            storePageNum: pageNum
          })
        }
        // console.log(res.data.itemList)
        const responseList = getGoodsData ? res.data.itemList : res.data.storeList
        if (responseList && responseList.length > 0) {
          if (getGoodsData) {
            that.setData({
              goodsList: refresh ? responseList : [...that.data.goodsList, ...responseList]
            })
          } else {
            that.setData({
              storeList: refresh ? responseList : [...that.data.storeList, ...responseList]
            })
          }
        } else {
          // 没有获取到数据，标记当前搜索列表没有更多数据了
          if (getGoodsData) {
            if (refresh) {
              that.setData({
                goodsList: [],
                goodsNoMoreData: true
              })
            } else {
              that.setData({
                goodsNoMoreData: true
              })
            }
          } else {
            if (refresh) {
              that.setData({
                storeList: []
              })
            } else {
              that.setData({
                storeNoMoreData: true
              })
            }
          }
        }
      } else {
        common.showRequestErrMsg(res)
      }
    },
      function () {
        // 接口请求完成的回调，标记为非请求状态
        if (getGoodsData) {
          that.setData({
            isLoadingGoodsBusy: false
          })
        } else {
          that.setData({
            isLoadingStoreBusy: false
          })
        }
      })
  },
  /**
   * 跳转到商品详情页
   * @param {object} e 参数对象 
   */
  goGoods(e) {
    common.gotoGoodsPage(e.currentTarget.dataset.item)
  },
  /**
   * 跳转到店铺详情页
   * @param {object} e 参数对象
   */
     // 判断模板 industryType   行业类型  1通用行业 2餐饮行业  3家电行业
     shopInfo(item) {
      var that = this
      var tempJson = {
          storeId: item.id,
          // storeId:33,
          xpoint: this.data.lat,
          ypoint: this.data.lon,
          xcFlag: '0' //是否含有携程门票 0不含有
      }
      common.get('/store/mainPageInfo', tempJson, res => {
          that.setData({
              industryType: res.data.store.industryType
          }, () => {
            if (this.data.industryType == 1) {
                const idx = item.storeId ? item.storeId : item.id // 兼容不同页面...
                const storeName = item.storeName ? item.storeName : item.name // 兼容不同页面...
                const storeImage = item.storeImage ? item.storeImage : ''
                var param = '';

                wx.navigateTo({
                    url: '/packageA/pages/Goods/storePage/index?id=' + idx +
                        '&storeName=' + storeName +
                        '&storeImage=' + storeImage + param
                })
            }else if (this.data.industryType == 2) {
             
                  wx.navigateTo({
                      url: `/packageC/pages/activeBei/activeBei?id=${item.id}&name=${item.storeName}`,
                  })
             
                
              }else if(this.data.industryType == 4){// 景区

                const idx = item.storeId ? item.storeId : item.id // 兼容不同页面...
                const storeTypeName = '景区景点';
                var type = 1;
                var storeType;
                wx.navigateTo({
                    url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + idx + '&moduleType=' + type +
                        '&storeTypeName=' + storeTypeName + '&storeType=' + storeType,
                })

              }else if(this.data.industryType == 5){// 场馆
                    const idx = item.storeId ? item.storeId : item.id // 兼容不同页面...
                    const storeTypeName = '文体场馆';
                    var type = 2;
                    var storeType;
                    wx.navigateTo({
                        url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + idx + '&moduleType=' + type +
                            '&storeTypeName=' + storeTypeName + '&storeType=' + storeType,
                    })
              }
            //   common.gotoStore(item)
           
          })
         
      })
  },
  toStore(e) {
    const id = e.currentTarget.dataset.item.id
    const storeTypeName = e.currentTarget.dataset.item.storeTypeName

    console.log(e.currentTarget.dataset.item,"55566666")
    this.shopInfo(e.currentTarget.dataset.item) 
    // if (storeTypeName === '景区景点') {
    //   wx.navigateTo({
    //     url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=1',
    //   })
    // } else if (storeTypeName === '文化馆' || storeTypeName === '美术馆' || storeTypeName === '博物馆') {
    //   wx.navigateTo({
    //     url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=2',
    //   })
    // } else {
      console.log("抬走换")
      // common.gotoStore(e.currentTarget.dataset.item)
    // }
  },
  /**
   * 加载更多数据
   */
  loadMore() {
    if (this.data.active === 0) {
      this.getSearchList(false, false)
    } else {
      this.getSearchList(false, true)
    }
  }
})